﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Diagnostics;

namespace ClockSync2
{

   public class RunningClock
    {
        
        public DateTime CurrentTime;
        private System.Windows.Forms.Timer runningTimer;
        
        public RunningClock()
        {
            CurrentTime = DateTime.Now; //get initial computer clock
            
            runningTimer = new System.Windows.Forms.Timer();
            runningTimer.Tick += new EventHandler(OnTimedEvent);
            runningTimer.Interval = 10;
        }

        public void run()
        {
            
            runningTimer.Start();
        }

        public void OnTimedEvent(object sender, EventArgs e)
        {

                CurrentTime = CurrentTime.AddMilliseconds(10);
                runningTimer.Interval = 10;

            runningTimer.Start();
        }

        public void setTime(byte[] time)
        {
            string[] t = utils.ByteToStrArray(time).Split(':');
            DateTime temp = new DateTime(CurrentTime.Year, CurrentTime.Month, CurrentTime.Day, int.Parse(t[0]), int.Parse(t[1]), int.Parse(t[2]), int.Parse(t[3]));
            this.CurrentTime = temp;
        }

        public void setTime(string[] time)
        {
            string[] t = time;
            DateTime temp = new DateTime(CurrentTime.Year, CurrentTime.Month, CurrentTime.Day, int.Parse(t[0]), int.Parse(t[1]), int.Parse(t[2]), int.Parse(t[3]));
            this.CurrentTime = temp;
        }


        public void setTime(int offset)
        {
            if (offset < 0)
            {
                this.CurrentTime = CurrentTime.AddMilliseconds(offset);
            }
            else
            {
                this.CurrentTime = CurrentTime.AddMilliseconds(offset);
            }
        }

        public string getTime()
        {
            string t = CurrentTime.Hour + ":" + CurrentTime.Minute + ":" + CurrentTime.Second + ":" + CurrentTime.Millisecond;
            return t;
        }

    }
}
